<?php
namespace app\common\library\xilujob\resume;


use app\common\library\xilujob\ResumeLibrary;
use app\common\model\xilujob\Area;
use app\common\model\xilujob\IndustryCategory;
use app\common\model\xilujob\JobCategory;
use app\common\model\xilujob\Resume;
use app\common\model\xilujob\ResumeJobIntention;
use app\common\model\xilujob\Salary;
use app\common\model\xilujob\WorkState;
use think\Model;
use function fast\array_get;

class JobIntention extends BaseModule {

    protected $name = ResumeLibrary::MODULE_JOB_INTENTION;

    public function onCreateResume(Resume $resume) {
//        $jobIntention = new ResumeJobIntention(['user_id'=>$resume->user_id, 'resume_id'=>$resume->id]);
//        $ret = $jobIntention->save();
//        if(!$ret) {
//            throw new ModuleException($jobIntention->getError());
//        }
    }

    public function add($data) {
        //throw new ModuleException("Module '{$this->name}' does not support add method");
        $cityId = array_get($data, 'fav_city_id');
        $exist = ResumeJobIntention::where('resume_id',$this->resume->id)->where("fav_category_id",$data['fav_category_id'])->where('fav_city_id',$cityId)->count();
        if($exist>0){
            throw new ModuleException("城市期望职位已存在，不要重复");
        }
        $city = Area::where('id',$cityId)->where('level',2)->field('id,pid')->find();

        if($city) {
            $data['fav_province_id'] = $city->pid;
        }
        return parent::add($data);
    }

    public function edit($data) {
        $cityId = array_get($data, 'fav_city_id');

        $exist = ResumeJobIntention::where('resume_id',$this->resume->id)->where("fav_category_id",$data['fav_category_id'])->where('id','neq',$data['id'])->where('fav_city_id',$cityId)->count();
        if($exist>0){
            throw new ModuleException("城市期望职位已存在，不要重复");
        }

        $city = Area::where('id',$cityId)->where('level',2)->field('id,pid')->find();
        if($city) {
            $data['fav_province_id'] = $city->pid;
        }
        return parent::edit($data);
    }

    public function del($id) {
        //throw new ModuleException("Module '{$this->name}' does not support del method");
        $ret = parent::del($id);
        return $ret;
    }

    /**
     * 获取所有字段的key
     * @return array
     */
    protected function keys() {
        return ['job_type','fav_category_id','expect_salary_id','fav_province_id','fav_city_id','industry_category_id'];
    }

    /**
     * 获取验证规则
     * @return array
     */
    protected function getRules() {
        return [
            'job_type' => ['in:1,2'],
            'fav_category_id' => function($value, $data) {
                $value = intval($value);
                if($value==0) {
                    return true;
                }
                if($value<1)
                    return '求职意向选择错误';
                $category = JobCategory::where('id',$value)->field('id,path')->find();
                if(!$category || count(explode(',',$category->path))<4 )
                    return '求职意向选择错误';
                return true;
            },
            'expect_salary_id' => function($value, $data) {
                $value = intval($value);
                if($value==0) {
                    return true;
                }
                if($value<1)
                    return '期望薪资选择错误';
                $exist = Salary::where('id',$value)->count();
                if($exist<1)
                    return '期望薪资选择错误';
                return true;
            },
            'industry_category_id' => function($value, $data) {
                $value = intval($value);
                if($value==0) {
                    return true;
                }
                if($value<1)
                    return '期望行业选择错误';
                $exist = IndustryCategory::where('id',$value)->count();
                if($exist<1)
                    return '期望行业选择错误';
                return true;
            },
//            'previous_job_category_id' => function($value, $data) {
//                $value = intval($value);
//                if($value==0) {
//                    return true;
//                }
//                if($value<1)
//                    return '之前职位选择错误';
//                $category = JobCategory::where('id',$value)->field('id,path')->find();
//                if(!$category || count(explode(',',$category->path))<4 )
//                    return '之前职位选择错误';
//                return true;
//            },
            'fav_city_id' => function($value, $data) {
                $value = intval($value);
                if($value==0) {
                    return true;
                }
                if($value<1)
                    return '意向城市选择错误';
                $exist = Area::where('id',$value)->where('level',2)->count();
                if($exist<1)
                    return '意向城市选择错误';
                return true;
            },
        ];
    }

    /**
     * 获得Model实例
     * @return Model
     */
    protected function getModel() {
        return new ResumeJobIntention();
    }

    /**
     * @return array score: 当前分数， max: 满分
     */
    public function getCompleteScore() {
        $max = 10;
        $count = ResumeJobIntention::where('resume_id',$this->resume->id)->count();
        $score = $count>0 ? 10 : 0;
        return ['score'=>$score, 'max'=>$max];
    }

}